package com.john.bms.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.john.bms.mapper.system.MenuMapper;
import com.john.bms.model.system.menu.*;
import com.john.bms.model.system.user.SysUser;
import com.john.bms.utils.AppSecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author:张家小帅哥
 * @Date:2020/6/6
 * @Version:1.0
 * @Description:TODO
 */
@Service
public class MenuService {

    @Resource
    private MenuMapper menuMapper;

    public List<MenuTreeBO> getTreeList() {

        SysUser userInfo = AppSecurityUtils.getUserInfo();
        List<MenuTreeBO> menuList = menuMapper.selectMenuListByRid(userInfo.getRId() + "");
        List<MenuTreeBO> list = new LinkedList<>();
        if (menuList == null && menuList.size() == 0) {
            return list;
        }
        List<String> ss = new LinkedList<>();
        ss.add("add");
        ss.add("update");
        //先找到所有的一级分类
        for (MenuTreeBO sysMenu : menuList) {
            if ("0".equals(sysMenu.getParentId())) {
                //是否有子菜单
                sysMenu.setHasChildren(1);
                MetaBO meta = new MetaBO();
                meta.setTitle(sysMenu.getTitle());
                meta.setIcon(sysMenu.getIcon());

                meta.setRoles(ss);
                sysMenu.setMeta(meta);
                list.add(sysMenu);
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (MenuTreeBO menu : menuList) {
            menu.setChildren(getChilde(menu.getId(), menuList));
        }

        return list;
    }

    private List<ChildernBO> getChilde(String id, List<MenuTreeBO> list) {
        //子菜单
        List<ChildernBO> childList = new LinkedList<>();
        List<String> ss = new LinkedList<>();
        ss.add("add");
        ss.add("update");
        for (MenuTreeBO menu : list) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            //int foo = Integer.parseInt(id);
            if (menu.getParentId().equals(id)) {

                ChildernBO cb = new ChildernBO();
                BeanUtils.copyProperties(menu, cb);

                MetaBO meta = new MetaBO();
                meta.setTitle(menu.getTitle());
                meta.setIcon(menu.getIcon());
                meta.setRoles(ss);
                cb.setMeta(meta);
                cb.setHasChildren(0);

                childList.add(cb);
            }
        }
        // 把子菜单的子菜单再循环一遍
//        for (ChildernBO childern : childList) {
//            sysMenu.setChildren(getChilde(sysMenu.getId(),list));
//        }
        // 递归退出条件
        if (childList.size() == 0) {
            List<ChildernBO> childList2 = new LinkedList<>();
            return childList2;
        }
        return childList;
    }

    public PageInfo<SysMenu> getPageList(MenuPage page) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<SysMenu> sysMenus = menuMapper.selectMenuPage(page);
        for (SysMenu sysMenu : sysMenus) {
            if (sysMenu.getParentId() == 0) {
                sysMenu.setHasChildren(1);
            }
        }
        PageInfo<SysMenu> pageInfo = new PageInfo<>(sysMenus);
        return pageInfo;
    }

    public boolean addMenu(SysMenu menu) {
        menu.setCreateTime(new Date());
        int insert = menuMapper.insertMenu(menu);
        return insert > 0 ? true : false;
    }

    public SysMenu getMenuInfo(Integer id) {
        SysMenu sysMenu = menuMapper.selectById(id);
        return sysMenu;
    }

    public boolean updateMenu(SysMenu menu) {
        menu.setUpdateTime(new Date());
        int result = menuMapper.updateById(menu);
        return result > 0 ? true : false;
    }

    public boolean deleteMenuById(Integer id) {
        int result = menuMapper.deleteById(id);
        return result > 0 ? true : false;
    }

    /**
     * @param id
     * @param status
     * @return
     * @Description: 修改菜单状态
     */
    public boolean getMenuStatus(String id, String status) {

        return menuMapper.updateStatus(id, status) > 0 ? true : false;
    }
}
